<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Takes;
use app\admin\model\Users;
class Take extends Common{
	public function _initialize(){
		parent::_initialize();
		$this->Takes = new Takes;
	}
	public function index($page=null,$key=null){
		$map['user_id'] = $this->User_id;
		if($this->request->isPost()){
			
			$page = (int)$page > 0 ? $page : 1;
			$lists = $this->Takes
				->where($map)
                ->order('create_time desc')
                ->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists['data'],'count'=>$lists['total'],'rel'=>1];
		}
		return $this->fetch();
	}
	public function setup($id=null){
		if($this->request->isPost()){
			$data = $this->request->post();
			if(empty($data['email']) && !is_email($data['email'])){
				return ['code'=>0,'msg'=>lang('Please enter the correct mailbox address.')];
			}
			if(!empty($data['email2'])){
				if(!is_email($data['email2'])){
					return ['code'=>0,'msg'=>lang('Please enter the correct mailbox address.')];
				}
			}
			$data['user_id'] = $this->User_id;
			if($id>0){
				$result = $this->Takes->isUpdate(true)->allowField(true)->update($data);
			}else{
				$data['create_time'] = time();
				$data['status'] = 1;
				$result = $this->Takes->data($data)->allowField(true)->insert($data);
			}
			if($result){
				return ['code'=>1,'msg'=>lang('The data is saved successfully!'),'url'=>U('index')];
			}
			return ['code'=>0,'msg'=>lang('Data save failed!')];
		}
		$row = [];
		$row['email'] = $this->user['email'];
		if($id>0){
			$mas['user_id'] = $this->User_id;
			$mas['id'] = $id;
			$row = db('takes')->where($mas)->find();
		}
		$types = $this->Takes->getTypeAttr(false);
		if(Users::isCustomer($this->User_id)){
			unset($types[1]);
		}
		$this->assign('types',$types);
		//$this->assign('weeks',$this->Takes->getWeeksAttr(false));
		$this->assign('row',$row);
		$this->assign('isCustomer',Users::isCustomer($this->User_id));
		return $this->fetch('form');
	}
	public function delete($id=null){
        if($id > 0 && $this->Takes->where('id',$id)->delete()){
            return ['code'=>1,'msg'=>lang('Data has been deleted')];
        }
        return ['code'=>0,'msg'=>lang('Delete data failure')];
    }
	public function editState(){
        $id=input('post.id');
        $status=input('post.status');
        if($this->Takes->isUpdate()->save(['status'=>$status,'id'=>$id])!==false){
            return ['code'=>1,'msg'=>lang('Modify state success')];
        }else{
            return ['code'=>0,'msg'=>lang('Modify state failure')];
        }
    }
    public function s(){
    	$array = array(
    		'香港'=>array(
    			array('form'=>array('name'=>'香港','latitude'=>'22.396428','longitude'=>'114.109497'),'to'=>array('name'=>'日本东京','latitude'=>'35.689487','longitude'=>'139.691706')),
    			array('form'=>array('name'=>'香港','latitude'=>'22.396428','longitude'=>'114.109497'),'to'=>array('name'=>'北京','latitude'=>'39.904211','longitude'=>'116.407395')),
    		),
    		'上海'=>array(
    			array('form'=>array('name'=>'上海','latitude'=>'31.2891','longitude'=>'121.4648'),'to'=>array('name'=>'日本东京','latitude'=>'35.689487','longitude'=>'139.691706')),
    			array('form'=>array('name'=>'上海','latitude'=>'31.2891','longitude'=>'121.4648'),'to'=>array('name'=>'北京','latitude'=>'39.904211','longitude'=>'116.407395')),
    		),
    	);
    }
}